package com.yc.sole.erp.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.sole.erp.dto.StockLogDto;
import com.yc.sole.erp.entity.StockLog;
import com.yc.sole.erp.mapper.StockLogMapper;
import com.yc.sole.erp.request.StockLogQueryReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2024-01-26
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class StockLogRepository extends ServiceImpl<StockLogMapper, StockLog> {

    /**
     * 新增
     *
     * @param stockLogDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveStockLog(StockLogDto stockLogDto) {
        StockLog entity = new StockLog();
        BeanUtil.copyProperties(stockLogDto, entity);
        this.save(entity);
    }

    /**
     * 通过Id修改
     *
     * @param stockLogDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateStockLogById(StockLogDto stockLogDto) {
        StockLog entity = new StockLog();
        BeanUtil.copyProperties(stockLogDto, entity);
        this.updateById(entity);
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteStockLogByIds(Collection<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 根据id获取
     *
     * @param ids
     * @return RestResult
     */
    public List<StockLogDto> selectStockLogByIds(Set<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return BeanUtil.copyToList(this.listByIds(ids), StockLogDto.class);
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return RestResult
     */
    public StockLogDto selectStockLogById(Long id) {
        return BeanUtil.copyProperties(this.getById(id), StockLogDto.class);
    }

    /**
     * 分页查询
     *
     * @param page
     * @return RestResult
     */
    public Paging<StockLogDto> selectStockLogPage(Paging page, StockLogQueryReq queryReq) {
        LambdaQueryWrapper<StockLog> wrapper = Wrappers.<StockLog>lambdaQuery()
                .eq(Objects.nonNull(queryReq.getProductId()), StockLog::getProductId, queryReq.getProductId())
                .and(CharSequenceUtil.isNotBlank(queryReq.getKeyword()),
                        w -> w.like(StockLog::getRemark, queryReq.getKeyword())
                )
                .orderByDesc(StockLog::getId);
        if (CollUtil.isNotEmpty(queryReq.getDateRange())
                && CharSequenceUtil.isNotBlank(queryReq.getDateRange().get(0))
                && CharSequenceUtil.isNotBlank(queryReq.getDateRange().get(1))) {
            wrapper.between(StockLog::getCreateTime, queryReq.getDateRange().get(0), queryReq.getDateRange().get(1));
        }
        return PageUtil.pageCopy(this.page(PageUtil.toMybatisPage(page), wrapper), StockLogDto.class);
    }
}
